package mazeBueno1;

import java.awt.geom.Rectangle2D;
import java.util.Random;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.*;

public class GridSquareComponent extends JPanel
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//static variables

	private int xsquares = 1;
	private int ysquares = 1;
	private JFrame g;
	private Maze maze;
	
	public GridSquareComponent(int xsquares, int ysquares, JFrame g) {
		maze = new Maze(xsquares,ysquares);
		this.xsquares = xsquares*2-1;
		this.ysquares = ysquares*2-1;
		this.g = g;
	}

	public void paintComponent(Graphics g)
	{
		/*
		Random a = new Random();
		//maze.generateMultipleChoice(a.nextInt(), 40);
		maze.generate(a.nextInt());
		g.setColor(Color.GREEN);
		g.fillRect(0, 0, this.g.getWidth(), this.g.getHeight());
		g.setColor(Color.WHITE);
		
		double xInc = (double)this.g.getWidth() / (double)(this.xsquares+10);
		double yInc = (double)this.g.getHeight() / (double)(this.ysquares+10);
		
		Rectangle2D rect; 
		Graphics2D g2 = (Graphics2D) g;
		g2.setStroke(new BasicStroke(1));
		
		for (int i = 0; i < this.xsquares; i++) {
			for (int j = 0; j < this.ysquares;j++) {
				if (maze.getSquare(i, j)) {
					rect = new Rectangle2D.Double(xInc*i, yInc*j, xInc, yInc);
					if (i == this.xsquares-5 || j == this.ysquares-5)
						g2.setColor(Color.RED);
					else
						g2.setColor(Color.WHITE);
					g2.draw(rect);
				}
					//g.fillRect(xInc*i, yInc*j, xInc, yInc);
			}
		}
		*/
		
		Random a = new Random();
		//maze.generateMultipleChoice(a.nextInt(),10);
		maze.generate(a.nextInt());
		
		// fill with the color you want
		g.setColor(Color.GREEN);
		g.fillRect(0, 0, this.g.getWidth(), this.g.getHeight());
		g.setColor(Color.WHITE);
		
		// go into Graphics2D for all the fine art, more options
		// optional, here I just get variable Stroke sizes
		Rectangle2D rect; 
		Graphics2D g2 = (Graphics2D) g;
		g2.setStroke(new BasicStroke(1));

		
		// that will have a little glitch with the integer math
		// the grid will have the bottom row slightly larger
		// to overcome that, you must use double
		
		double rowH = getHeight() / (double)this.ysquares;
		double colW = getWidth() / (double)this.xsquares;
		for (int i = 0; i < this.xsquares; i++) {
			for (int j = 0; j < this.ysquares; j++) {
				if (maze.getSquare(i, j)) {
					rect = new Rectangle2D.Double(colW*i, rowH*j, colW, rowH);
					g2.fill(rect);
				}
			}
		}
	}
}



